/**   
 * @Title: DaletouAgp.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: (双色球模块主ActivityGroup,承载其他功能型Activity) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-7-5 下午12:18:41  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.CurrentLotteryNumInfo;
import com.esun.model.DaletouInfo;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.hemaicenteragp.DaletouHemaiListAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.daletouagp.DaletouChooseAct;
import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LatestdetailsAct;
import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LotteryInfoListAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryRecordDaLeTouAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.trackingrecord.TrackingRecordDaLeTouAct;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: DaletouAgp
 * @Description: (大乐透模块主ActivityGroup,承载其他功能型Activity)
 * @author: City Jiangsy@500wan.com
 * @date: 2011-7-5 下午12:18:41
 * 
 */
public class DaletouAgp extends ActivityGroup implements OnClickListener {

	private ImageButton btnDaletouAgpCancel;
	private ImageButton btnDaletouAgpMenu;

	private LinearLayout linearMenuBg;
	private TextView tvTitle;

	private Dialog menuDialog = null;
	private Button menuBtnChoose;
	private Button menuBtnHemai;
	private Button menuBtnLotteryRecord;
	private Button menuBtnLotteryInfo;
	private Button menuBtnIntroduction;
	private Button menuBtnZhuihaoRecord;

	private Bundle bundleSetCurrentTab;

	private boolean isInChooseView = true;
	private boolean isRebuy;
	/********** 1.6.0kael修改 **************************/
	/** 承载最新详情和更多期次的按钮的LinearLayout */
	private LinearLayout lltitle;
	/** 最新详情按钮 */
	private LinearLayout lllatestdetails;
	/** 更多期次按钮 */
	private LinearLayout llmoretime;
	/** 最新详情图标 */
	private ImageView ivlatestdetails;
	/** 最新详情字样 */
	private TextView tvlatestdetails;
	/** 更多其次图标 */
	private ImageView ivmoretime;
	/** 更多期次字样 */
	private TextView tvmoretime;
	/** 动态加载Activity的ScrollView */
	private FrameLayout container;
	Bundle hemaiBundle;
	Bundle bundle;
	public static final int CHOOSE = 0;
	public static final int HEMAI = 1;
	public static final int LOTTERYRECORD = 2;
	public static final int LOTTERYINFO = 3;
	public static final int INTRODUCE = 4;
	public static final int ZHUIHAORECORD = 5;
	public static final int LATESTDETAILS = 6;
	public static final int MORETIME = 7;
	private int actid = CHOOSE;

	private Message message;

	/************************************************/

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.daletou_agp);
		uiInit();
		initHandler();
		EDebug.o(this, "start");

		bundleSetCurrentTab = this.getIntent().getExtras();
		isRebuy = bundleSetCurrentTab.getBoolean(Config.IS_REBUY);
		hemaiBundle = new Bundle();
		hemaiBundle.putString("lot_id", Config.DALETOU);
		bundle = new Bundle();
		bundle.putString("lotid", Config.DALETOU);
		bundle.putString("type", "1");

		try {
			actid = bundleSetCurrentTab.getInt(Config.CURRENT_TAB_NUM);
		} catch (Exception e) {
			actid = CHOOSE;
		}
		addViewActivity(actid);

		menuDialog.dismiss();
		if (isRebuy) {
			btnDaletouAgpCancel.setVisibility(View.INVISIBLE);
			btnDaletouAgpMenu.setImageResource(R.drawable.cancel_rebuy_25x25);
			if (DaletouInfo.getDaletouInfo().getLotteryList().size() == 0) {
				DaletouChooseAct.isChooseView = true;
				message = handlerDaletouAgp.obtainMessage(IN_CHOOSE_VIEW, null);
				handlerDaletouAgp.sendMessage(message);
			} else {
				message = handlerDaletouAgp.obtainMessage(IN_LIST_VIEW, null);
				handlerDaletouAgp.sendMessage(message);
			}
		}

	}

	private void uiInit() {

		btnDaletouAgpCancel = (ImageButton) findViewById(R.id.daletouagp_btn_cancel);
		btnDaletouAgpCancel.setOnClickListener(this);

		btnDaletouAgpMenu = (ImageButton) findViewById(R.id.daletouagp_btn_menu);
		btnDaletouAgpMenu.setOnClickListener(this);

		tvTitle = (TextView) findViewById(R.id.daletou_agp_tv_title);
		lltitle = (LinearLayout) findViewById(R.id.daletouagp_webview_ll_title);
		lllatestdetails = (LinearLayout) findViewById(R.id.daletouagp_ll_latestdetails);
		lllatestdetails.setOnClickListener(this);
		llmoretime = (LinearLayout) findViewById(R.id.daletouagp_ll_moretime);
		llmoretime.setOnClickListener(this);
		ivlatestdetails = (ImageView) findViewById(R.id.daletouagp_iv_latestdetails);
		ivmoretime = (ImageView) findViewById(R.id.daletouagp_iv_moretime);
		tvlatestdetails = (TextView) findViewById(R.id.daletouagp_tv_latestdetails);
		tvmoretime = (TextView) findViewById(R.id.daletouagp_tv_moretime);
		container = (FrameLayout) findViewById(R.id.daletouagp_containerBody);
		setMenuDialog();

	}

	private void setMenuDialog() {
		menuDialog = new Dialog(this, R.style.dialogNoFloating);
		menuDialog.getWindow().setContentView(R.layout.shuangseqiuagp_dialog_menu);

		menuBtnChoose = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_choose);
		menuBtnChoose.setOnClickListener(this);

		menuBtnHemai = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_hemai);
		menuBtnHemai.setOnClickListener(this);

		menuBtnLotteryRecord = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_lotteryRecord);
		menuBtnLotteryRecord.setOnClickListener(this);

		menuBtnLotteryInfo = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_lotteryInfo);
		menuBtnLotteryInfo.setOnClickListener(this);

		menuBtnIntroduction = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_introduction);
		menuBtnIntroduction.setOnClickListener(this);

		menuBtnZhuihaoRecord = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_zhuihaoRecord);
		menuBtnZhuihaoRecord.setOnClickListener(this);

		linearMenuBg = (LinearLayout) menuDialog.getWindow().findViewById(R.id.shuangseqiuagp_dialog_menu_linear_bg);
		linearMenuBg.setOnClickListener(this);

		menuDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

				if (isInChooseView) {
					DaletouChooseAct.isChooseView = true;
				}
			}
		});

		isCanShake();

		menuDialog.show();

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.daletouagp_btn_cancel:
			DaletouAgp.this.finish();
			break;

		case R.id.daletouagp_btn_menu:

			if (isRebuy) {
				reBuyCancel();
			} else {
				setMenuDialog();
			}
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_choose:
			actid = CHOOSE;
			addViewActivity(actid);
			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_hemai:
			actid = HEMAI;
			addViewActivity(actid);
			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_lotteryRecord:

			actid = LOTTERYRECORD;
			addViewActivity(actid);

			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_lotteryInfo:
			actid = LATESTDETAILS;
			addViewActivity(actid);
			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_introduction:
			actid = INTRODUCE;
			addViewActivity(actid);
			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagpdialogmenu_btn_zhuihaoRecord:
			actid = ZHUIHAORECORD;
			addViewActivity(actid);

			menuDialog.dismiss();
			break;

		case R.id.shuangseqiuagp_dialog_menu_linear_bg:
			menuDialog.dismiss();
			break;

		case R.id.daletouagp_ll_latestdetails:

			actid = LATESTDETAILS;
			addViewActivity(actid);

			break;

		case R.id.daletouagp_ll_moretime:

			actid = MORETIME;
			addViewActivity(actid);

			break;

		default:
			break;
		}

	}

	private void reBuyCancel() {

		UserInfo.getUserInfo().setRebuy(false);
		DaletouAgp.this.finish();
		DaletouInfo.getDaletouInfo().getLotteryList().clear();

	}

	private boolean isLogin() {
		if (UserInfo.getUserInfo().getUserSession().length() > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_MENU) {
			setMenuDialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {

		return true;
	}

	@Override
	protected void onPause() {

		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {

		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());

	}

	private void isCanShake() {
		if (DaletouChooseAct.isChooseView) {
			isInChooseView = true;
		} else {
			isInChooseView = false;
		}
	}

	/** title change 2011-7-27 city */
	public static Handler handlerDaletouAgp;
	public static final int IN_CHOOSE_VIEW = 100021;
	public static final int IN_LIST_VIEW = 100022;

	private void initHandler() {
		handlerDaletouAgp = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				EDebug.o(DaletouAgp.this, "handler star");
				switch (msg.what) {
				case IN_CHOOSE_VIEW:
					tvTitle.setText("大乐透投注");
					break;

				case IN_LIST_VIEW:
					tvTitle.setText("大乐透投注列表");
					break;

				default:
					break;
				}
			}

		};
	}

	public void addViewActivity(int viewId) {
		switch (viewId) {
		case CHOOSE:
			DaletouChooseAct.showType = 0;
			lltitle.setVisibility(View.GONE);
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("choose", new Intent(DaletouAgp.this, DaletouChooseAct.class)).getDecorView());
			if (isInChooseView) {
				DaletouChooseAct.isChooseView = true;
				message = handlerDaletouAgp.obtainMessage(IN_CHOOSE_VIEW, null);
				handlerDaletouAgp.sendMessage(message);
			} else {
				message = handlerDaletouAgp.obtainMessage(IN_LIST_VIEW, null);
				handlerDaletouAgp.sendMessage(message);
			}
			Message msg = DaletouChooseAct.handlerNormalBuy.obtainMessage(DaletouChooseAct.DO_REFRESH, null);
			DaletouChooseAct.handlerNormalBuy.sendMessage(msg);
			break;
		case HEMAI:
			lltitle.setVisibility(View.GONE);
			tvTitle.setText("大乐透合买列表");
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("hemai", new Intent(DaletouAgp.this, DaletouHemaiListAct.class).putExtras(hemaiBundle)).getDecorView());
			break;
		case LOTTERYRECORD:

			actid = LATESTDETAILS;
			if (isLogin()) {
				lltitle.setVisibility(View.GONE);
				tvTitle.setText("大乐透购彩记录");
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity("record", new Intent(DaletouAgp.this, LotteryRecordDaLeTouAct.class)).getDecorView());
				message = LotteryRecordDaLeTouAct.handlerGetDaLeTouLotteryRecord.obtainMessage(Config.DO_REFRESH);
				LotteryRecordDaLeTouAct.handlerGetDaLeTouLotteryRecord.sendMessage(message);
			} else {
				startActivity(new Intent(DaletouAgp.this, LoginAct.class));
			}

			break;
		case INTRODUCE:
			lltitle.setVisibility(View.GONE);
			menuDialog.dismiss();
			tvTitle.setText("大乐透玩法介绍");

			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity("introduction",
					new Intent(DaletouAgp.this, HelpAct.class).putExtra("url", Config.DALETOU_INTRODUCE_WEB_ADD).putExtra("title", Config.NULLMARK).putExtra("havetitle", Config.NUM0)).getDecorView());
			break;
		case ZHUIHAORECORD:
			if (isLogin()) {
				lltitle.setVisibility(View.GONE);
				tvTitle.setText("大乐透追号记录");
				container.addView(getLocalActivityManager().startActivity("zhuihaoRecord", new Intent(DaletouAgp.this, TrackingRecordDaLeTouAct.class).putExtra(Key.KEY_LOTID, Config.DALETOU))
						.getDecorView());
				message = TrackingRecordDaLeTouAct.trackingRecordHandler.obtainMessage(Config.DO_REFRESH);
				TrackingRecordDaLeTouAct.trackingRecordHandler.sendMessage(message);
			} else {
				startActivity(new Intent(DaletouAgp.this, LoginAct.class));
			}
			break;
		case LATESTDETAILS:
			lltitle.setVisibility(View.VISIBLE);
			lllatestdetails.setBackgroundResource(R.drawable.anull_icon);
			llmoretime.setBackgroundResource(R.drawable.btn_default_title_right_88x30);
			ivlatestdetails.setImageResource(R.drawable.icon_pressed_this_13x23);
			ivmoretime.setImageResource(R.drawable.icon_default_all_24x24);
			tvlatestdetails.setTextColor(getResources().getColor(R.color.white));
			tvmoretime.setTextColor(getResources().getColor(R.color.lotteryinfodefault));
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity(Key.KEY_LATESTDETAILS, new Intent(DaletouAgp.this, LatestdetailsAct.class).putExtras(bundle)).getDecorView());
			break;
		case MORETIME:
			lltitle.setVisibility(View.VISIBLE);
			lllatestdetails.setBackgroundResource(R.drawable.btn_default_title_left_88x30);
			llmoretime.setBackgroundResource(R.drawable.anull_icon);
			ivlatestdetails.setImageResource(R.drawable.icon_default_this_13x23);
			ivmoretime.setImageResource(R.drawable.icon_pressed_all_24x24);
			tvlatestdetails.setTextColor(getResources().getColor(R.color.lotteryinfodefault));
			tvmoretime.setTextColor(getResources().getColor(R.color.white));
			container.removeAllViews();
			container.addView(getLocalActivityManager().startActivity(Key.KEY_MORETIME, new Intent(DaletouAgp.this, LotteryInfoListAct.class).putExtra(Key.KEY_LOTID, Config.DALETOU)).getDecorView());
			break;
		}
	}

	public void jumpToCurrentInfo(Message msg) {
		Intent intent = new Intent();

		intent.setClass(DaletouAgp.this, LatestdetailsAct.class);
		lllatestdetails.setBackgroundResource(R.drawable.anull_icon);
		llmoretime.setBackgroundResource(R.drawable.btn_default_title_right_88x30);
		ivlatestdetails.setImageResource(R.drawable.icon_pressed_this_13x23);
		ivmoretime.setImageResource(R.drawable.icon_default_all_24x24);
		tvlatestdetails.setTextColor(getResources().getColor(R.color.white));
		tvmoretime.setTextColor(getResources().getColor(R.color.lotteryinfodefault));
		container.removeAllViews();
		container.addView(getLocalActivityManager().startActivity(Key.KEY_LATESTDETAILS, intent).getDecorView());
		LatestdetailsAct.handlerLatestsetailsAct.sendMessage(msg);
		String expect = msg.getData().getString(Key.KEY_EXPECT);
		if (expect.equals(CurrentLotteryNumInfo.getCurrentLotteryNumInfo().getLastexpect())) {
			tvlatestdetails.setText(Language.TEXT_LATEST_DETIAL_LOTTERY);
		} else {
			tvlatestdetails.setText(Language.TEXT_DETIAL_LOTTERY);
		}
	}
}
